#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/adc.h"


void EnableADC() {
    //
    // Display the setup on the console.
    //
    //UARTprintf("ADC ->\n");
    //UARTprintf("  Type: Single Ended\n");
    //UARTprintf("  Samples: One\n");
    //UARTprintf("  Update Rate: 250ms\n");
    //UARTprintf("  Input Pin: AIN0/PE7\n\n");

    //
    // The ADC0 peripheral must be enabled for use.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

    //
    // For this example ADC0 is used with AIN0 on port E7.
    // The actual port and pins used may be different on your part, consult
    // the data sheet for more information.  GPIO port E needs to be enabled
    // so these pins can be used.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

    //
    // Select the analog ADC function for these pins.
    // Consult the data sheet to see which functions are allocated per pin.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_7);

    //
    // Enable sample sequence 3 with a processor signal trigger.  Sequence 3
    // will do a single sample when the processor sends a signal to start the
    // conversion.  Each ADC module has 4 programmable sequences, sequence 0
    // to sequence 3.  This example is arbitrarily using sequence 3.
    //
	//change sequencer to 1
    ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);

    //
    // Configure step 0 on sequence 3.  Sample channel 0 (ADC_CTL_CH0) in
    // single-ended mode (default) and configure the interrupt flag
    // (ADC_CTL_IE) to be set when the sample is done.  Tell the ADC logic
    // that this is the last conversion on sequence 3 (ADC_CTL_END).  Sequence
    // 3 has only one programmable step.  Sequence 1 and 2 have 4 steps, and
    // sequence 0 has 8 programmable steps.  Since we are only doing a single
    // conversion using sequence 3 we will only configure step 0.  For more
    // information on the ADC sequences and steps, reference the datasheet.
    //
	//#define CHAN_ACCELX     ADC_CTL_CH8
	//#define CHAN_ACCELY     ADC_CTL_CH9
	//#define CHAN_ACCELZ     ADC_CTL_CH21
    ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_CH8 | ADC_CTL_IE );
    ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_CH9 | ADC_CTL_IE );
    ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_CH21 | ADC_CTL_IE |
                             ADC_CTL_END);
    //
    // Since sample sequence 3 is now configured, it must be enabled.
    //
    ADCSequenceEnable(ADC0_BASE, 1);

    //
    // Clear the interrupt status flag.  This is done to make sure the
    // interrupt flag is cleared before we sample.
    //
 
   ADCIntClear(ADC0_BASE, 1);
}

unsigned long ReadADC(unsigned long *pADCvalue) {
    //
    // This array is used for storing the data read from the ADC FIFO. It
    // must be as large as the FIFO for the sequencer in use.  This example
    // uses sequence 3 which has a FIFO depth of 1.  If another sequence
    // was used with a deeper FIFO, then the array size must be changed.
    //
    unsigned long ulADC0_Value[4];


    //
    // Sample AIN0 forever.  Display the value on the console.
    //

        //
        // Trigger the ADC conversion.
        //
        ADCProcessorTrigger(ADC0_BASE, 1);

        //
        // Wait for conversion to be completed.
        //
        while(!ADCIntStatus(ADC0_BASE, 1, false))
        {
        }

        //
        // Read ADC Value.
        //
        ADCSequenceDataGet(ADC0_BASE, 1, ulADC0_Value);

        //
        // Display the AIN0 (PE7) digital value on the console.
        //
        pADCvalue[0] = ulADC0_Value[0];
		pADCvalue[1] = ulADC0_Value[1];
		pADCvalue[2] = ulADC0_Value[2];
		pADCvalue[3] = ulADC0_Value[3];
		
		return true;

        //
        // This function provides a means of generating a constant length
        // delay.  The function delay (in cycles) = 3 * parameter.  Delay
        // 250ms arbitrarily.
        //
        //SysCtlDelay(SysCtlClockGet() / 12);

}



